package com.car.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.entity.CarData;
import com.car.mapper.CarDataMapper;
import com.car.result.Result;
import com.car.service.CarDataService;
import com.car.vo.CarDataQueryVo;
import com.car.vo.ResultVo;
import com.car.vo.ResultVo2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/api/car")
public class CarDataController {

    @Resource
    private CarDataService carDataService;

    @Resource
    private CarDataMapper carDataMapper;

    /**
     * 分页查询数据
     */
    @PostMapping("/page")
    public Result<Page<CarData>> page(@RequestBody CarDataQueryVo carData) {
        return Result.ok(carDataService.selectPage(carData));
    }

    @GetMapping("/brandNameList")
    public Result<List<String>> brandNameList() {
        LambdaQueryWrapper<CarData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(CarData::getBrandName);
        List<CarData> carDataList = carDataService.list(queryWrapper);
        return Result.ok(carDataList.stream().map(CarData::getBrandName).distinct().collect(Collectors.toList()));
    }

    @GetMapping("/dateList")
    public Result<List<String>> dateList() {
        LambdaQueryWrapper<CarData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(CarData::getDate);
        List<CarData> carDataList = carDataService.list(queryWrapper);
        return Result.ok(carDataList.stream().map(CarData::getDate).distinct().collect(Collectors.toList()));
    }

    @GetMapping("/seriesNameList")
    public Result<List<String>> seriesNameList() {
        LambdaQueryWrapper<CarData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(CarData::getSeriesName);
        List<CarData> carDataList = carDataService.list(queryWrapper);
        return Result.ok(carDataList.stream().map(CarData::getSeriesName).distinct().collect(Collectors.toList()));
    }

    /**
     * 各具体车型的平均最低价和最高价
     */
    @GetMapping("/seriesNamePrice")
    public Result<List<ResultVo2>> seriesNamePrice() {
        return Result.ok(carDataMapper.seriesNamePrice());
    }

    /**
     * 各品牌各月份平均排行和销量
     */
    @GetMapping("/brandNameRankCount/{brandName}")
    public Result<List<ResultVo2>> brandNameRankCount(@PathVariable String brandName) {
        return Result.ok(carDataMapper.brandNameRankCount(brandName));
    }

    /**
     * 各具体车型各月份均价与销量排行榜
     */
    @GetMapping("/datePriceCount/{date}")
    public Result<List<ResultVo2>> datePriceCount(@PathVariable String date) {
        return Result.ok(carDataMapper.datePriceCount(date));
    }

    /**
     * 各品牌销量占比
     */
    @GetMapping("/brandNameCount")
    public Result<List<ResultVo>> brandNameCount() {
        return Result.ok(carDataMapper.brandNameCount());
    }

    /**
     * 各车型各城市销量
     */
    @GetMapping("/seriesNameCity/{seriesName}")
    public Result<List<ResultVo>> seriesNameCity(@PathVariable String seriesName) {
        return Result.ok(carDataMapper.seriesNameCity(seriesName));
    }


}
